import tagpot, MySQLdb, string
cursor = tagpot.dbConnect()

def pot(words):
    for t in words:
        
        # term freq.
        q ="select count from termfreq where term = '%s'"%(t)
        cursor.execute(q)
        ok = cursor.fetchall()
        if ok is ():
            q = "insert into termfreq(term, count) values('%s',%s)" % (t, 1)
        else:
            count = ok[0][0] + 1
            q = "update termfreq set count = %s where term = '%s'" %(count, t)
        cursor.execute(q)
        
        for i in words:
            if t == i:
                continue
            if t == '' or i == '':
                continue
            
            q = "select freq from lang where target='%s' and candidate='%s'" %(t,i)
            cursor.execute(q)
            check = cursor.fetchall()
            if check is ():
                q = "insert into lang(target,candidate,freq) values ('%s','%s',%s)" % (t,i,1)
                cursor.execute(q)
            else:
                freq = check[0][0] + 1
                q = "update lang SET freq = %s WHERE target = '%s' and candidate = '%s'" % (freq, t, i)
                cursor.execute(q)
            

if __name__=='__main__':
    q = "select distinct anchor from anc";
    cursor.execute(q)
    source = cursor.fetchall()
    total = []
    for r in source:
        list = []
        q = "select ko,en from anc where anchor = '%s'" % (r[0])
        cursor.execute(q)
        li= cursor.fetchall()
        en = li[0][1]
        s_en = en.split(",")
        
        for s in s_en:
            if s == '':
                continue
            s = s.replace("'", "")
            list.append(s.lower().strip())
        
        for i in li:
            s_ko = i[0].split(",")
            for v in s_ko:
                if v == '':
                    continue
                v = v.replace("'", "")
                list.append(v.lower().strip())
        
        pot(list)        
        #total.append(list)

    